<!-- 友链编辑弹窗 -->
<template>
  <el-dialog
    :title=" '修改到期时间'"
    :visible="visible"
    width="750px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :close-on-click-modal="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-form-item label="到期时间:">
        <el-date-picker
          type="datetime"
          class="ele-fluid"
          v-model="form.valid_end_time"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择到期时间"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
    export default {
        name: "userMachineEdit",
        props: {
            // 弹窗是否打开
            visible: Boolean,
            // 修改回显的数据
            data: Object
        },
        data() {
            return {
                // 表单数据
                form: Object.assign({}, this.data),
                // 表单验证规则
                rules: {
                },
                // 提交状态
                loading: false,
            };
        },

        watch: {
            data() {
                if (this.data) {
                    this.form = Object.assign({}, this.data);
                    this.isUpdate = true;
                } else {
                    this.form = {};
                    this.isUpdate = false;
                }
            }
        },
        methods: {
            /* 保存编辑 */
            save() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        this.loading = true;
                        this.$http
                            .post("/BoxYunUserMachine/editUserMachine", this.form)
                            .then(res => {
                                this.loading = false;
                                if (res.data.code === 0) {
                                    this.$message.success(res.data.msg);
                                    if (!this.isUpdate) {
                                        this.form = {};
                                    }
                                    this.updateVisible(false);
                                    this.$emit("done");
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            })
                            .catch(e => {
                                this.loading = false;
                                this.$message.error(e.message);
                            });
                    } else {
                        return false;
                    }
                });
            },
            /* 更新visible */
            updateVisible(value) {
                this.$emit("update:visible", value);
            },

            confirmClose() {
                this.$confirm("确定要返回吗？", "温馨提示")
                    .then(() => {
                        this.updateVisible(false);
                        if (!this.isUpdate) {
                            this.form = {};
                        }
                    })
                    .catch(() => {
                    });
            }
        }
    };
</script>

<style scoped></style>
